/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "ObjectWindow.h"

#include <QtGui/QLayout>
#include <QtGui/QMouseEvent>
#include <QtGui/QApplication>

#include "NgExecutor.h"
#include "DataSerializer.h"

namespace PQBuilder
{
	ObjectCreateTree::ObjectCreateTree(QWidget *parent)
	{
		this->setHeaderLabel(QObject::tr("Create Object"));
		init();
	}

	void ObjectCreateTree::init()
	{
		_pointLightItem = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QObject::tr("Point Light")));
		this->addTopLevelItem(_pointLightItem);

		_directLightItem = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QObject::tr("Directional Light")));
		this->addTopLevelItem(_directLightItem);

		_spotLightItem = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QObject::tr("Spot Light")));
		this->addTopLevelItem(_spotLightItem);

		_rigidBodyItem = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QObject::tr("Rigid Body")));
		this->addTopLevelItem(_rigidBodyItem);

		_planeColliderItem = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QObject::tr("Plane Collider")));
		this->addTopLevelItem(_planeColliderItem);

		_boxColliderItem = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QObject::tr("Box Collider")));
		this->addTopLevelItem(_boxColliderItem);

		_sphereColliderItem = new QTreeWidgetItem((QTreeWidget*)0, QStringList(QObject::tr("Sphere Collider")));
		this->addTopLevelItem(_sphereColliderItem);
	}

	void ObjectCreateTree::mousePressEvent(QMouseEvent *evt)
	{
		QTreeWidget::mousePressEvent(evt);
		if (evt->button() == Qt::LeftButton){
			_dragStartPosition = evt->pos();
		}
	}

	void ObjectCreateTree::mouseMoveEvent(QMouseEvent *evt)
	{
		if (!(evt->buttons() & Qt::LeftButton)){
			return;
		}
		
		if ((evt->pos() - _dragStartPosition).manhattanLength()< QApplication::startDragDistance()){
			return;
		}

		PQCore::SerializeDatagram *datagram=0;
		QTreeWidgetItem *item=this->currentItem();

		// LT_POINT = 0,
		// LT_DIRECTIONAL = 1,
		// LT_SPOTLIGHT = 2
		 if(item==_pointLightItem){
			datagram=GNgineExec::genCreateLightObject(0);
		}else if(item==_directLightItem){   
			datagram=GNgineExec::genCreateLightObject(1);
		}else if(item==_spotLightItem){   
			datagram=GNgineExec::genCreateLightObject(2);
		}else if(item==_rigidBodyItem){
			datagram=GNgineExec::genCreateObject(PQEngine::OBJECT_TYPE_COMPONENT_RIGIDBODY);
		}else if(item==_planeColliderItem){
			datagram=GNgineExec::genCreateObject(PQEngine::OBJECT_TYPE_COMPONENT_PLANE_COLLIDER);
		}else if(item==_boxColliderItem){
			datagram=GNgineExec::genCreateObject(PQEngine::OBJECT_TYPE_COMPONENT_BOX_COLLIDER);
		}else if(item==_sphereColliderItem){
			datagram=GNgineExec::genCreateObject(PQEngine::OBJECT_TYPE_COMPONENT_SPHERE_COLLIDER);
		}
		
		if(datagram==0){
			return;
		}

		QByteArray bytes=QByteArray(datagram->data(),datagram->length());
		QDrag *drag = new QDrag(this);
		QMimeData *mimeData = new QMimeData;

		assert(false);
		/*
		mimeData->setData(QString::fromStdString(PQEngine::GameEngine::COMMAND_DATA_TYPE),bytes);
		drag->setMimeData(mimeData);
		Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);

		delete datagram;
		*/
	}


	ObjectWindow::ObjectWindow(QWidget *parent)
	{
		_treeWidget = new ObjectCreateTree(this);

		QVBoxLayout *boxlayout = new QVBoxLayout(this);
		boxlayout->setMargin(0);
		boxlayout->addWidget(_treeWidget);
		boxlayout->setStretch(0,0);
		boxlayout->setStretch(1,1);
	}

	ObjectWindow::~ObjectWindow()
	{
	}
}
